package com.rd.student.jiaji.class2.huangyiming.Day0728;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;

public class RandomStudents {
    public static void main(String[] args) {
        int n = 2;
        List<String> Students = new ArrayList<>();
        Students.add("jhy");
        Students.add("wyf");
        Students.add("lx");
        Students.add("dyk");
        Students.add("sr");
        Students.add("hym");
        Students.add("sy");
        List<String> randomStudents = getRandomStudents(Students, n);
        for (String student : randomStudents) {
            System.out.println(student);
        }
    }

    public static List<String> getRandomStudents(List<String> allStudents, int n) {
        List<String> randomStudents = new ArrayList<>();
        if (allStudents.size() <= n) {
            return allStudents;
        }
        HashSet<Integer> indices = new HashSet<>();
        Random random = new Random();
        while (indices.size() < n) {
            int index = random.nextInt(allStudents.size());
            indices.add(index);
        }
        for (int index : indices) {
            randomStudents.add(allStudents.get(index));
        }
        return randomStudents;
    }
}
